package com.yogee.topseller.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.activity.PayActivity;
import com.yogee.topseller.activity.tianyan.ZqxxDetailActivity;
import com.yogee.topseller.bean.GrfxBean;
import com.yogee.topseller.bean.ZqxxBean;
import com.yogee.topseller.utils.Constants;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 个人风险
 * @auathor ${zwj}
 * @date 2017/6/26.
 */

public class ZqxxListAdapter extends RecyclerView.Adapter<ZqxxListAdapter.ItemHolder> {

    private Context context;
    private List<ZqxxBean.Item> beans = new ArrayList<>();


    private PayActivity.Listener listener;

    public ZqxxListAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<ZqxxBean.Item> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    public void setListener(PayActivity.Listener listener){
        this.listener = listener;
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_xzcf_item, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, final int position) {
        if(position==0)
            holder.view_line.setVisibility(View.GONE);
        else
            holder.view_line.setVisibility(View.VISIBLE);
        final ZqxxBean.Item bean = beans.get(position);
        holder.tv_title.setText(bean.getBondName());
        holder.tv_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                context.startActivity(new Intent().setClass(context, ZqxxDetailActivity.class)
                        .putExtra("detail",(Serializable) beans.get(position)));
            }
        });
    }

    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.view_line)
        View view_line;
        @BindView(R.id.tv_title)
        TextView tv_title;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     *
     * @param s
     * @return 获得链接
     */
    public List<String> getLink(final String s)
    {
        String regex;
        final List<String> list = new ArrayList<String>();
        regex = "href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>";
        final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
        final Matcher ma = pa.matcher(s);
        while (ma.find())
        {
            list.add(ma.group());
        }
        return list;
    }


}